package pub.cleangao;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {

        try {
            List<String> original = ExcelProcess.readColumnFromSheet("E:/998_temp_run/original.xlsx", 0, 6, 1);
            List<String> originalMoney = ExcelProcess.readColumnFromSheet("E:/998_temp_run/original.xlsx", 0, 12, 1);
            for (int i = 0; i < originalMoney.size(); i++) {
                originalMoney.set(i, originalMoney.get(i).substring(1));
            }
            System.out.println(original);
            List<String> finance = ExcelProcess.readColumnFromSheet("E:/998_temp_run/finance.xlsx", 0, 1, 5);
            List<String> financeMoney = ExcelProcess.readColumnFromSheet("E:/998_temp_run/finance.xlsx", 0, 5, 5);
            System.out.println(finance);
            List<String> originalMore = new ArrayList<>();
            List<String> financeMore = new ArrayList<>();
            BigDecimal sumOriginalDiff = new BigDecimal("0");
            BigDecimal sumOriginalSame = new BigDecimal("0");
            for (int i = 0; i < original.size(); i++) {
                String item = original.get(i);
                item = item.substring(1, "TC20250522lIS9m2".length() + 1);
                original.set(i, item);
                if (!finance.contains(item)) {
                    originalMore.add(item);
                    originalMoney.set(i, originalMoney.get(i));
                    sumOriginalDiff = sumOriginalDiff.add(new BigDecimal(originalMoney.get(i)));
                } else {
                    sumOriginalSame = sumOriginalSame.add(new BigDecimal(originalMoney.get(i)));
                }
            }

            BigDecimal sumFinanceDiff = new BigDecimal("0");
            BigDecimal sumFinanceSame = new BigDecimal("0");
            for (int i = 0; i < finance.size(); i++) {
                String item = finance.get(i);
                if (!original.contains(item)) {
                    financeMore.add(item);
                    sumFinanceDiff = sumFinanceDiff.add(new BigDecimal(financeMoney.get(i)));
                } else {
                    sumFinanceSame = sumFinanceSame.add(new BigDecimal(financeMoney.get(i)));
                }
            }

            System.out.println("微信订单多余记录（系统导出表中不存在）==================================================");
            System.out.println("商户订单号取前16位");
            System.out.println(originalMore);
            System.out.println(originalMore.size());
            System.out.println("系统订单多余记录（微信交易记录中不存在）==================================================");
            System.out.println(financeMore);
            System.out.println(financeMore.size());
            System.out.println("系统订单总记录：" + finance.size() + ", 原始订单总记录:" + original.size());
            System.out.println("不同ID，微信后台数据合计金额: " + sumOriginalDiff.toString());
            System.out.println("不同ID，系统后台数据合计金额: " + sumFinanceDiff.toString());
            System.out.println("相同ID，微信后台数据合计金额: " + sumOriginalSame.toString());
            System.out.println("相同ID，系统后台数据合计金额: " + sumFinanceSame.toString());
            Map<String, BigDecimal> sameIdMap = new HashMap<>();
            Map<String, String> sameIdDiffMoneyMap = new HashMap<>();
            for (int i = 0; i < original.size(); i++) {
                String item = original.get(i);
                if (finance.contains(item)) {
                    int financeIndex = finance.indexOf(item);
                    BigDecimal originalMoneyItem = new BigDecimal(originalMoney.get(i));
                    BigDecimal financeMoneyItem = new BigDecimal(financeMoney.get(financeIndex));
                    if (originalMoneyItem.compareTo(financeMoneyItem) == 0) {
                        sameIdMap.put(item, financeMoneyItem);
                    } else {
                        sameIdDiffMoneyMap.put(item, "wx:" + originalMoneyItem + " sys:" + financeMoneyItem);
                    }
                }
            }

            System.out.println("相同金额单号，共" + sameIdMap.size() + "条记录：");
            System.out.println(sameIdMap);
            System.out.println("不同金额单号，共" + sameIdDiffMoneyMap.size() + "条记录：");
            System.out.println(sameIdDiffMoneyMap);
            System.out.println("微信合计金额：" + originalMoney.stream().mapToDouble(o -> {
                return new BigDecimal(o).doubleValue();
            }).sum());
            System.out.println("系统合计金额：" + financeMoney.stream().mapToDouble(o -> {
                return new BigDecimal(o).doubleValue();
            }).sum());

            System.out.println("===================================问题出现在相同订单号，金额不同===================================");
            for (int i = 0; i < original.size(); i++) {
                String item = original.get(i);
                int financeIndex = finance.indexOf(item);
                if (financeIndex > -1) {
                    BigDecimal originalMoneyItem = new BigDecimal(originalMoney.get(i));
                    originalMoneyItem.setScale(2, RoundingMode.HALF_UP);
                    BigDecimal financeMoneyItem = new BigDecimal(financeMoney.get(financeIndex));
                    financeMoneyItem.setScale(2, RoundingMode.HALF_UP);
                    if (originalMoneyItem.compareTo(financeMoneyItem) == 0) {
                        sameIdMap.put(item, financeMoneyItem);
                    } else {
                        System.out.println(item + ", 微信：" + originalMoneyItem.toString() + ", 系统：" + financeMoneyItem.toString());
                        sameIdDiffMoneyMap.put(item, "wx:" + originalMoneyItem + " sys:" + financeMoneyItem);
                    }
                }
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}